package com.shz.utils;

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBufferUtils;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;

public class ImageUtil {

    public static String convertImageUrlToBase64(String imageUrl) throws IOException {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<byte[]> response = restTemplate.getForEntity(imageUrl, byte[].class);
        byte[] imageData = response.getBody();

        // Convert to base64 encoded string
        String base64Image = "data:image/png;base64," + Base64.getEncoder().encodeToString(imageData);

        return base64Image;
    }

    public static void main(String[] args) {
        try {
            String imageUrl = "https://shzzz.oss-cn-hangzhou.aliyuncs.com/dangjian-images/0.jpg";
            String base64Image = convertImageUrlToBase64(imageUrl);
            System.out.println(base64Image);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}